//
//  MemoryData.m
//  ios-supplier
//
//  Created by Benlai on 15/8/18.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "MemoryData.h"
#import "JFBOrderGoodsProductDS.h"

@implementation MemoryData


static MemoryData *memoryData = nil;

+(MemoryData *)sharedMemoryData
{
    if (!memoryData) {
        memoryData = [[self allocWithZone:NULL] init];
    }
    return memoryData;
}

-(NSMutableArray *)shoppingCarList
{
    if (_shoppingCarList == nil) {
        _shoppingCarList = [NSMutableArray array];
    }
    return _shoppingCarList;
}

-(NSInteger)getCarGoodsNumber
{
    NSInteger count = 0;
    for (JFBOrderGoodsProductDS *item in _shoppingCarList) {
        count += item.buyCount;
    }
    return count;
}

-(double)getTotalPrice
{
    double price = 0;
    for (JFBOrderGoodsProductDS *item in _shoppingCarList) {
        CGFloat product = [item.productPrice floatValue];
        price +=  product * item.buyCount;
    }
    return price;
}

-(NSInteger)getGoodsBuyCount: (NSInteger) product_id
{
    for (JFBOrderGoodsProductDS *item in _shoppingCarList) {
        if (item.productId == product_id)
        {
            return item.buyCount;
        }
    }
    return 0;
}


-(void)changeGoodsBuyCount: (NSInteger)product_id isAdd: (BOOL)add
{
    for (int i = 0; i < _shoppingCarList.count; i++){
        JFBOrderGoodsProductDS *item = _shoppingCarList[i];
        if (item.productId == product_id) {
            if (add) {
                
                item.buyCount++;
                
            } else  {
                //默认购买1件商品
                if (item.buyCount == 1) {
                    [_shoppingCarList removeObject: item];
                    return;
                }
                if (item.buyCount == 0) {
                    return;
                }
                item.buyCount--;
//                item.buyCount--;//默认购买2件商品
//                if (item.buyCount == 0) {
//                    [_shoppingCarList removeObject: item];
//                    return;
//                }
            }
        }
    }
}


-(void)setGoodsBuyCount: (NSInteger)product_id withBuyNum: (NSInteger)buyNum
{
    NSMutableArray *mutableArray = [NSMutableArray new];
    
    for (JFBOrderGoodsProductDS * item in _shoppingCarList) {
        if (item.productId == product_id) {
            if (buyNum == 0) {
                [mutableArray addObject:item];
                
            } else {
                
                item.buyCount = buyNum;
            }
        }
    }
    
    [_shoppingCarList removeObjectsInArray:mutableArray];
}

@end
